package com.zhiyou.aifeng.smart_machine

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.sunfusheng.GlideImageView
import com.zhiyou.aifeng.common.helper.get
import com.zhiyou.aifeng.common.helper.getString
import com.zhiyou.aifeng.common.helper.toNotInt
import com.zhiyou.aifeng.common_ui.base.*
import com.zhiyou.aifeng.common_ui.oss.scaleImage
import com.zhiyou.aifeng.common_ui.utils.bindLifeCycle
import com.zhiyou.aifeng.common_ui.utils.subscribeByFinally
import com.zhiyou.aifeng.model.RankData
import com.zhiyou.aifeng.viewmodel.RankViewModel
import kotlinx.android.synthetic.main.activity_rank.*
import kotlinx.android.synthetic.main.layout_list.*
import net.idik.lib.slimadapter.SlimAdapter

class RankActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_rank)
        setTransparentAndToolbar(isDarkFont = false)
        initTitle()

        getData(isLoading = true)
    }

    override fun getViewModel() = get<RankViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        initList()
    }

    private fun initList() {
        swipe_refresh.refresh { getData(1, false) }
        recycle_list.apply {
            loadLinear(baseContext, swipe_refresh)

            mAdapter = SlimAdapter.create()
                .register<RankData>(R.layout.item_rank_list) { data, injector ->

                    val index = mList.indexOf(data)
                    val isLast = index == mList.size - 1

                    injector.text(R.id.item_rank_num, (index + 4).toString())
                        .text(R.id.item_rank_name, data.realname)
                        .text(R.id.item_rank_total, "累计完成${data.allot_cnt}单")
                        .with<GlideImageView>(R.id.item_rank_img) {
                            it.loadCircleImage(data.avatar.scaleImage())
                        }
                        .visibility(
                            R.id.item_rank_divider,
                            if (isLast) View.GONE else View.VISIBLE
                        )
                }
                .attachTo(this)
        }
    }

    @SuppressLint("SetTextI18n")
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().getRankList(getString("userId"), isLoading)
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    val items = it.rankList
                    mList.apply {
                        clear()
                        mList.addItems(items.filterIndexed { index, _ -> index > 2 })
                    }
                    mAdapter.updateData(mList)

                    if (items.size > 0) {
                        val item = items[0]
                        rank_img1.loadCircleImage(item.avatar.scaleImage())
                        rank_name1.text = item.realname
                        rank_total1.text = "累计完成${item.allot_cnt}单"
                    }

                    if (items.size > 1) {
                        val item = items[1]
                        rank_img2.loadCircleImage(item.avatar.scaleImage())
                        rank_name2.text = item.realname
                        rank_total2.text = "累计完成${item.allot_cnt}单"
                    }

                    if (items.size > 2) {
                        val item = items[2]
                        rank_img3.loadCircleImage(item.avatar.scaleImage())
                        rank_name3.text = item.realname
                        rank_total3.text = "累计完成${item.allot_cnt}单"
                    }

                    val inner = it.myRank?.rank_num.toNotInt()
                    if (inner < 1) {
                        rank_hint.text = "当前你已完成${it.myRank?.allot_cnt ?: 0}单，暂未上榜"
                    } else {
                        rank_hint.text = "当前你已完成${it.myRank?.allot_cnt ?: 0}单，排行第${inner}名"
                    }
                },
                onFinally = { swipe_refresh.isRefreshing = false }
            )
    }

}